package com.shtel.paas.monitorcenter.config.management.alarm.config.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shtel.paas.monitorcenter.config.management.alarm.config.entity.AlarmConfigDto;
import com.shtel.paas.monitorcenter.config.management.alarm.config.entity.AlarmConfigEntity;
import com.shtel.paas.monitorcenter.config.management.alarm.config.entity.AlarmConfigParam;
import com.shtel.paas.monitorcenter.config.management.alarm.config.entity.AlarmRulesDto;
import com.shtel.paas.monitorcenter.config.management.alarm.config.entity.AlarmRulesEntity;
import com.shtel.paas.monitorcenter.config.management.alarm.config.entity.AlarmRulesParam;
import com.shtel.paas.monitorcenter.config.management.alarm.config.mapper.WarningKpiDao;
import com.shtel.paas.monitorcenter.config.management.alarm.config.mapper.warningConfigDao;
import com.shtel.paas.monitorcenter.config.management.alarm.config.service.IWarningConfigService;

import javax.xml.crypto.Data;

@Service
public class WarningConfigService implements IWarningConfigService {

	@Autowired
	warningConfigDao dao;

	@Autowired
	WarningKpiDao warningKpiDao;

	@Override
	public PageInfo<AlarmConfigDto> selectAll(String monitoringIndex, Integer page, Integer pageSize) {
		PageHelper.startPage(page, pageSize);
		List<String> indexList = dao.selectMonitorIndex(monitoringIndex);
		PageInfo<AlarmConfigDto> pageInfo = new PageInfo(indexList);
		// Long total = pageInfo.getTotal();
		int pages = pageInfo.getPages();
		List<AlarmConfigDto> dtoList = new ArrayList<AlarmConfigDto>();

		List<AlarmConfigEntity> resultList = dao.selectAll(monitoringIndex);

		for (String index : indexList) {
			AlarmConfigDto dto = new AlarmConfigDto();
			for (AlarmConfigEntity entity : resultList) {

				if (entity.getMonitorIndex().equals(index)) {
					dto.setDesc(entity.getMonitorIndex());
					dto.setName(entity.getKpi());
					dto.setCmd(entity.getCmd());
					dto.setType(entity.getType());
					if (entity.getLevels().equals("1")) {
						dto.setLv1min(entity.getMin());
						dto.setLv1max(entity.getMax());
					} else if (entity.getLevels().equals("2")) {
						dto.setLv2min(entity.getMin());
						dto.setLv2max(entity.getMax());
					} else if (entity.getLevels().equals("3")) {
						dto.setLv3min(entity.getMin());
						dto.setLv3max(entity.getMax());
					}

				}
			}
			dtoList.add(dto);
		}
		// pageInfo.setList(dtoList);
		PageInfo<AlarmConfigDto> repageInfo = new PageInfo(dtoList);
		repageInfo.setTotal(indexList.size());
		repageInfo.setPages(pages);
		return repageInfo;
	}

	@Override
	public List<String> selectMonitorIndex() {

		return dao.selectMonitorIndex(null);
	}

	@Override
	public int updateConfig(AlarmConfigParam alarmConfigParam) {
		int i = 0;
		List<AlarmRulesParam> list = dao.selectMonitorIndexbyDesc(alarmConfigParam.getDesc());
		if(null==list || list.size()==0){
			return 0;
		}
		AlarmRulesParam rule = list.get(0);
		AlarmRulesEntity param = new AlarmRulesEntity();
		param.setDesc(alarmConfigParam.getDesc());

		if (alarmConfigParam.getWarningLv1ValueHigh() != "" || alarmConfigParam.getWarningLv1ValueLow() != "") {
			param.setLevel("1");
			param.setMax(alarmConfigParam.getWarningLv1ValueHigh());
			param.setMin(alarmConfigParam.getWarningLv1ValueLow());
			boolean ex = false;
			for (AlarmRulesParam map : list) {
				String level = map.getLevel();
				if(null != level && "1".equals(level)){
					ex=true;
				}
			}

			if(ex){
				i = dao.updateConfig(param);
			}else{

				AlarmRulesParam params = new AlarmRulesParam();
				params.setMax(alarmConfigParam.getWarningLv1ValueHigh());
				params.setMin(alarmConfigParam.getWarningLv1ValueLow());
				params.setDesc(alarmConfigParam.getDesc());
				params.setLevel("1");
				params.setKpi(rule.getName());
				params.setCode(rule.getCode());
				params.setType(rule.getType());
				params.setAlarm_frequency_id(rule.getAlarm_frequency_id());
				params.setUpdateUser(rule.getUpdateUser());

				i= dao.insertAlarmRules(params);
			}


		}
		if (alarmConfigParam.getWarningLv2ValueHigh() != "" || alarmConfigParam.getWarningLv2ValueLow() != "") {
			param.setLevel("2");
			param.setMax(alarmConfigParam.getWarningLv2ValueHigh());
			param.setMin(alarmConfigParam.getWarningLv2ValueLow());


			boolean ex = false;
			for (AlarmRulesParam map : list) {
				String level = map.getLevel();
				if(null != level && "2".equals(level)){
					ex=true;
				}
			}

			if(ex){
				i = dao.updateConfig(param);
			}else{
				AlarmRulesParam params = new AlarmRulesParam();
				params.setMax(alarmConfigParam.getWarningLv2ValueHigh());
				params.setMin(alarmConfigParam.getWarningLv2ValueLow());
				params.setDesc(alarmConfigParam.getDesc());
				params.setLevel("2");
				params.setKpi(rule.getName());
				params.setCode(rule.getCode());
				params.setType(rule.getType());
				params.setAlarm_frequency_id(rule.getAlarm_frequency_id());
				params.setUpdateUser(rule.getUpdateUser());
				i= dao.insertAlarmRules(params);
			}
		}
		if (alarmConfigParam.getWarningLv3ValueHigh() != "" || alarmConfigParam.getWarningLv3ValueLow() != "") {
			param.setLevel("3");
			param.setMax(alarmConfigParam.getWarningLv3ValueHigh());
			param.setMin(alarmConfigParam.getWarningLv3ValueLow());

			boolean ex = false;
			for (AlarmRulesParam map : list) {
				String level = map.getLevel();
				if(null != level && "3".equals(level)){
					ex=true;
				}
			}

			if(ex){
				i = dao.updateConfig(param);
			}else{
				AlarmRulesParam params = new AlarmRulesParam();
				params.setMax(alarmConfigParam.getWarningLv3ValueHigh());
				params.setMin(alarmConfigParam.getWarningLv3ValueLow());
				params.setDesc(alarmConfigParam.getDesc());
				params.setLevel("3");
				params.setKpi(rule.getName());
				params.setCode(rule.getCode());
				params.setType(rule.getType());
				params.setAlarm_frequency_id(rule.getAlarm_frequency_id());
				params.setUpdateUser(rule.getUpdateUser());

				i= dao.insertAlarmRules(params);
			}
		}
		return i;
	}

	@Override
	public int insertConfig(AlarmRulesDto alarmConfigParam) {
		int i = 0;
		if (alarmConfigParam.getWarningLv1ValueHigh() != "" || alarmConfigParam.getWarningLv1ValueLow() != "") {
			AlarmRulesParam param = new AlarmRulesParam();
			param.setDesc(alarmConfigParam.getDesc());
			param.setKpi(alarmConfigParam.getKpi());
			param.setMax(alarmConfigParam.getWarningLv1ValueHigh());
			param.setMin(alarmConfigParam.getWarningLv1ValueLow());
			param.setLevel("1");
			i = dao.insertConfig(param);
		}
		if (alarmConfigParam.getWarningLv2ValueHigh() != "" || alarmConfigParam.getWarningLv2ValueLow() != "") {
			AlarmRulesParam param = new AlarmRulesParam();
			param.setDesc(alarmConfigParam.getDesc());
			param.setKpi(alarmConfigParam.getKpi());
			param.setMax(alarmConfigParam.getWarningLv2ValueHigh());
			param.setMin(alarmConfigParam.getWarningLv2ValueLow());
			param.setLevel("2");
			i = dao.insertConfig(param);
		}
		if (alarmConfigParam.getWarningLv3ValueHigh() != "" || alarmConfigParam.getWarningLv3ValueLow() != "") {
			AlarmRulesParam param = new AlarmRulesParam();
			param.setDesc(alarmConfigParam.getDesc());
			param.setKpi(alarmConfigParam.getKpi());
			param.setMax(alarmConfigParam.getWarningLv3ValueHigh());
			param.setMin(alarmConfigParam.getWarningLv3ValueLow());
			param.setLevel("3");
			i = dao.insertConfig(param);
		}

		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("kpi_name", alarmConfigParam.getKpi());
		paramMap.put("kpi_type", alarmConfigParam.getKpiType());
		paramMap.put("kpi_cmd", alarmConfigParam.getKpiCmd());
		paramMap.put("kpi_desc", alarmConfigParam.getDesc());

		i = i + warningKpiDao.insertWarningKpi(paramMap);

		return i;
	}

	@Override
	public int deleteConfig(String desc) {
		warningKpiDao.deleteWarningKpi(desc);
		dao.deleteConfig(desc);

		return 1;
	}


}
